# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

  def setup_list
    if !session[:cart]
      session[:cart] = []
    end
    if !session[:customer]
      session[:customer]
    end
  end

  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => 'f30dde69676e6c856d0fd21717a68a9b'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password
  
  #   Returns a 2 dimensional array of rows and columns from a csv string    
    def decode_csv(data)
        newdata = []
        for row in data do
            newRow = []
            multiline = false
            prevtext = ""   
            for col in row do
                #puts "col is:(" + col +")"
                if multiline
                    #puts "  is multiline"
                    if col[col.length - 1] == "\""[0] or (col[col.length - 2] == "\""[0] and col[col.length - 1] == "\n"[0])
                        #puts "found end of multiline, adding:" + prevtext +","+ col[0, col.length - 2]
                        multiline = false               
                        newRow.push(prevtext +","+ stripTripleQuotes(col[0, col.length - 2]))
                    else
                        #puts "multiline not terminated"
                        prevtext += "," + stripTripleQuotes(col)
                    end
                else
                    #   Remove extra whitespace at the start of line
                    if col[0] == " "[0] and col[1] == "\""[0]
                        col = col[1, col.length - 1]
                        #puts "col changed to (" + col + ")"
                    end
                    #   Remove newline at end of line.
                    if col[col.length-1] == "\n"[0] and col[col.length-2] == "\""[0]
                        col = col[0, col.length - 1]
                        #puts "col changed to (" + col + ")"
                    end
                    #puts "does " + col[0].to_s() + " equal "+ ("\""[0]).to_s() +" and " + col[col.length - 1].to_s() + " not equal "+ ("\""[0]).to_s()
                    #   Looks for an unterminated string, for instance: "one such as this
                    if col[0] ==  "\""[0] and col[col.length - 1] != "\""[0]
                        multiline = true
                        prevtext += stripTripleQuotes(col[1, col.length])
                    elsif col[0] ==  "\""[0] and (col[col.length - 1] == "\""[0] and col[col.length - 2] == "\""[0] and col[col.length - 3] == "\""[0] and col[col.length - 4] != "\""[0])
                        multiline = true
                        prevtext += stripTripleQuotes(col[1, col.length])
                    else
                        #   Remove quotes encapsulating a field.
                        if col[0] == "\""[0] and col[col.length - 1] == "\""[0]
                           col = col[1, col.length - 2]
                        end
                        if col[0] == " "[0]
                            col = col[1, col.length - 1]
                        end
                        if col[col.length-1] == "\n"[0]
                            col = col[0, col.length - 1]
                        #puts "col changed to (" + col + ")"
                        end
                        #   Replace sequences of 3 quotes with just one.
                        newcol = stripTripleQuotes(col)
                        #puts "single line, adding " + newcol
                        newRow.push(newcol)
                    end       
                end       
            end
            newdata.push(newRow)
        end
        #puts 'finally, our result is...'
        #puts newdata[0]
        return newdata
    end

    #Replaces any quotes " with three """
    def escapeQuotes(x)
        result = ""
        while 1
            index1 = x.index(/"/)
            if index1        
                result += x[0..index1] + "\"\""
                x = x[(index1 + 1)..x.length-1]
            else
                result += x
                break
            end
        end
        return result
    end

    #Turns groups of three quotes """ into a single "
    def stripTripleQuotes(s)
        newcol = ""
        block = 0
        for ii in 0..s.length-1
            if block > 0
                block -= 1
            else
                if s[ii] == "\""[0] and s[ii + 1] == "\""[0] and s[ii + 2] == "\""[0]
                    newcol += "\""
                    block = 2           
                else
                    newcol += s[ii].chr
                end
            end
        end
        return newcol
    end

# The reason the field values are being placed in an array first before being
# put into a string is because you can call .uniq on the array to remove repeats
  def customerAutocompleteData
    customerFnames1 = []
    customerLnames1 = []
    customerFnames = ""
    customerLnames = ""
    customerIDs = ""
    Customer.find(:all).each do |c|
        if c.first_name
            customerFnames1.push(c.first_name)
        end
        if c.last_name
            customerLnames1.push(c.last_name)
        end
        if c.studentid
            customerIDs += c.studentid.to_s + "☺"
        end
    end
    if customerFnames1 != []
        customerFnames1 = customerFnames1.uniq
        customerFnames1.each do |fn|
            customerFnames += fn + "☺"
        end
    end
    if customerLnames1 != []
        customerLnames1 = customerLnames1.uniq
        customerLnames1.each do |ln|
            customerLnames += ln + "☺"
        end
    end    
    return [customerFnames, customerLnames, customerIDs]
  end
  
  def equipmentAutocompleteData
        equipNames = []
        equipDescs = []
        equipCategs = []    
        equipmentNames = ""
        equipmentDescriptions = ""
        equipmentCategories = ""
        Equipment.find(:all).each do |e|
            if(e.name)
                equipNames.push(e.name)
            end
            if e.description
                equipDescs.push(e.description)
            end
            c = Category.find_by_id(e.category)
            if c
                equipCategs.push(c.name)
            end
        end 
        equipNames = equipNames.uniq
        equipDescs = equipDescs.uniq
        equipCategs = equipCategs.uniq    
        if equipNames
            equipNames.each do |en|
                equipmentNames += en + "☺"
            end
        end
        if equipDescs
            equipDescs.each do |ed|
                equipmentDescriptions += ed + "☺"
            end
        end
        if equipCategs
            equipCategs.each do |ec|
                equipmentCategories += ec + "☺"
            end       
        end
   #     equipmentNames = equipmentNames[0..equipmentNames.length-2]
   #     equipmentDescriptions = equipmentDescriptions[0..equipmentDescriptions.length-2]    
   #     equipmentCategories = equipmentCategories[0..equipmentCategories.length-2]
        return [equipmentNames, equipmentDescriptions, equipmentCategories]
    end
  
  private
  def authorize
	  unless Employee.find_by_id(session[:employee_id])
	  	flash[:notice] = "Please log in"
	  	redirect_to(:controller => "login", :action => "login")
	  end
  end
end
